"""
1. socket创建一个套接字
2. bind绑定ip和port
3. listen使套接字设置为被动模式
4. accept等待客户端的链接
5. recv/send接收发送数据
"""
import socket
from utils import decode_data

tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

tcp_server_socket.bind(("127.0.0.1", 8080))

tcp_server_socket.listen(128)

print("------------------------服务器已经开启！等待客户端接入!------------------------")

tcp_client, client_addr =tcp_server_socket.accept()

print("有新的客户端来咯：", client_addr)

while True:

    client_data = tcp_client.recv(1024)
    if client_data:
        print("收到客户端数据:", client_data.decode("GBK"))
        tcp_client.send("消息已经收到".encode("UTF-8"))
    else:
        print(client_data, type(client_data))
        print("客户端已经断开连接：", client_addr)
        tcp_client.close()
        break

tcp_server_socket.close()